using System;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;
using System.Configuration;
using Microsoft.SharePoint.Utilities;
using System.Reflection;

namespace CKS.FormsBasedAuthentication
{   

    class ConfigManagementFeatureReceiver : SPFeatureReceiver
    {
        private Guid _solutionID = new Guid("EC69235D-7FA1-4382-B7F8-0E7029DAEDA9");
        private Dictionary<String, Guid> _features = new Dictionary<String, Guid>();

        public ConfigManagementFeatureReceiver()
        {
            _features.Add("Configuration", new Guid("4B7AA2AF-3CB0-4ce6-9A59-2758AD9CBFBC")); // fba configuration                                                            
        }

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            // activate sub 
            try
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                foreach (KeyValuePair<String, Guid> feature in _features)
                {
                    web.Features.Add(feature.Value);
                }
            }
            catch (Exception ex)
            {
                TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.CriticalEvent, Guid.NewGuid(), Assembly.GetExecutingAssembly().FullName, "CKS.FormsBasedAuthentication", "AdminManagement", ex.Message);
            }
        }   

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties) 
        {
            // deactivate sub features
            try
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                foreach (KeyValuePair<String, Guid> feature in _features)
                {
                    web.Features.Remove(feature.Value);
                }
            }
            catch (Exception ex)
            {
                TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.CriticalEvent, Guid.NewGuid(), Assembly.GetExecutingAssembly().FullName, "CKS.FormsBasedAuthentication", "AdminManagement", ex.Message);
            }        
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties) 
        {
            // TODO: Should not be invoked if already registered
            TraceProvider.RegisterTraceProvider();

            // install sub features
            try
            {
                foreach (KeyValuePair<String, Guid> feature in _features)
                {
                    SPFarm.Local.FeatureDefinitions.Add(feature.Key, _solutionID, true);
                }
            }
            catch (Exception ex)
            {
                TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.CriticalEvent, Guid.NewGuid(), Assembly.GetExecutingAssembly().FullName, "CKS.FormsBasedAuthentication", "AdminManagement", ex.Message);
            }
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties) 
        {
            // TODO: Should not be invoked if already deregistered
            TraceProvider.UnregisterTraceProvider();

            // uninstall sub features
            try
            {
                foreach (KeyValuePair<String, Guid> feature in _features)
                {
                    SPFarm.Local.FeatureDefinitions.Remove(feature.Key, true);
                }
            }
            catch (Exception ex)
            {
                TraceProvider.WriteTrace(0, TraceProvider.TraceSeverity.CriticalEvent, Guid.NewGuid(), Assembly.GetExecutingAssembly().FullName, "CKS.FormsBasedAuthentication", "AdminManagement", ex.Message);
            }
        }

               
    }
}
